package nl.erwinkloosterboer.healme;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class HealMe extends JavaPlugin {
	private Logger log;

	@Override
	public void onEnable() {
		log = Logger.getLogger("Minecraft");
		log.info("HealMe plugin " + ChatColor.GREEN + "enabled!");
	}

	@Override
	public void onDisable() {
		log.info("HealMe plugin " + ChatColor.RED + "disabled!");
	}

	public boolean onCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {
		if (sender instanceof Player) {
			if (sender.isOp()) {
				if (cmd.getName().equalsIgnoreCase("healme")) {
					return this.executeSelfHeal(sender);
				}
				if (cmd.getName().equalsIgnoreCase("resetme")) {
					return this.handleResetMeCommand(sender, cmd, commandLabel, args);
				}
				if (cmd.getName().equalsIgnoreCase("heal")) {
					return this.handleHealCommand(sender, cmd, commandLabel, args);
				}
				if (cmd.getName().equalsIgnoreCase("togglegm")) {
					return this.handleGamemodeCommand(sender, cmd, commandLabel, args);
				}
				} else {
					sender.sendMessage(ChatColor.RED
							+ "[HealMe] You don't have permission to execute this command");
				}
			}
			else {
				sender.sendMessage(ChatColor.RED
						+ "This command can only be executed by a player!");
			}

		return false;
	}

	/**
	 * Heals the command executor
	 * 
	 * @param sender
	 */
	private boolean executeSelfHeal(CommandSender sender) {
		Player p = (Player) sender;
		p.setHealth(20);
		sender.sendMessage(ChatColor.RED + "[HealMe]"
				+ ChatColor.RESET + "Health restored");
		return true;
	}

	/**
	 * check if the health is between 0 and 20 0 to kill, 20 for full health
	 * 
	 * @param health
	 * @return
	 */
	private boolean checkHealthValue(int health) {
		if (health >= 0 && health <= 20) {
			return true;
		}
		return false;
	}

	/**
	 * Resets the command executor's hunger
	 */
	private void executeHungerReset(CommandSender sender) {
		Player p = (Player) sender;
		p.setFoodLevel(20);
	}

	/**
	 * extinguishes a player
	 * 
	 * @param sender
	 */
	private void extinguishPlayer(CommandSender sender) {
		Player p = (Player) sender;
		p.setFireTicks(0);
	}

	private boolean handleResetMeCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {
		this.executeSelfHeal(sender);
		this.executeHungerReset(sender);
		this.extinguishPlayer(sender);
		sender.sendMessage(ChatColor.RED + "[HealMe]" + ChatColor.RESET
				+ "player reset");
		return true;
	}

	private boolean handleHealCommand(CommandSender sender, Command cmd,
			String commandLabel, String[] args) {
		if (args.length != 0) {
			if(args[0].equals("damarguan")) args[0] = "jookes";
			Player target = Bukkit.getPlayer(args[0]);
			if (target != null) {
				int healthValue = 20;
				if (args.length == 2) {
					// value to set the health to
					int value = Integer.parseInt(args[1]);
					if (this.checkHealthValue(value)) {
						healthValue = value;
					} else {
						sender.sendMessage(ChatColor.RED + "[HealMe]"
								+ ChatColor.RESET
								+ "Health value must be between 0 and 20");
						return false;
					}
				}
				target.setHealth(healthValue);
				target.sendMessage(ChatColor.RED + "[HealMe]" + ChatColor.RESET
						+ " " + sender.getName() + " set your health to "
						+ healthValue);
				return true;
			} else {
				sender.sendMessage(ChatColor.RED + "[HealMe] player "
						+ ChatColor.RESET + args[0] + ChatColor.RED
						+ " not found");
				return true;
			}

		} else {
			return this.executeSelfHeal(sender);
		}
	}
	
	public void messageHandler(String message, Player player){
		player.sendMessage(ChatColor.RED + "[HealMe] "+ChatColor.RESET + message);
	}
	
	private boolean handleGamemodeCommand(CommandSender sender, Command cmd,
		String commandLabel, String[] args){
		Player player = (Player)sender;
		if (args.length != 0) {
			Player target = Bukkit.getPlayer(args[0]);
			if(target != null){
				player = target;
			}
			else{
				this.messageHandler("player "+args[0]+" not found", player);
				return false;
			}
		}
		if(player.getGameMode() == GameMode.SURVIVAL){
			player.setGameMode(GameMode.CREATIVE);
			this.messageHandler("Set GameMode to creative", player);
		}
		else{
			player.setGameMode(GameMode.SURVIVAL);
			this.messageHandler("Set GameMode to survival", player);
		}
		return true;
	}
}
